package avicit.im.lantop.basic.archiveclassmanager.service.api;


import avicit.cbb.techclass.tabledefine.commtablemanager.domain.CbbTabColCode;
import avicit.im.lantop.basic.archiveclassmanager.domain.LantopArchiveClass;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.core.restclient.RestClient;
import avicit.platform6.core.restclient.RestClientUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ArchiveclassmanagerServiceApi {


    /**
     * 服务编码
     */
    private static final String SERVICE_CODE = "filebasicmanager";

    private static final String BASE_PATH = "/api/avicit/im/lantop/basic/archiveclassmanager/action/rest/ArchiveclassmanagerServiceRest";

    @Autowired
    private RestClient restClient;


    /**
     * 通过path拿到pathName及仅部门可见设置
     * @param path
     * @return
     */
    public Map<String, Object> getPathNameByPath(String path) {
        String url = BASE_PATH + "/getPathNameByPath";
        ResponseMsg<Map<String, Object>> responseMsg = restClient.doPost(SERVICE_CODE, url, path, new ParameterizedTypeReference<ResponseMsg<Map<String, Object>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 获得最大节点顺序值
     * @param id
     * @return
     */
    public int getMaxNodeSortNumber(String id) {
        String url = BASE_PATH + "/getMaxNodeSortNumber";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 更新排序号
     * @param id
     * @param nodeSortNumber
     * @param o
     * @param o1
     * @param o2
     */
    public void updateData(String id, String nodeSortNumber, Object o, Object o1, Object o2) {
        String url = BASE_PATH + "/updateData";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("nodeSortNumber",nodeSortNumber);
        map.put("o",o);
        map.put("o1",o1);
        map.put("o2",o2);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
    }

    /**
     * 通过查询条件,返回查询到的节点路径集合
     * @author 钟一华
     * @param value
     * @return
     * @throws Exception String
     * @throws
     */
    public List<String> getNodePathsByNodeName(String value) {
        String url = BASE_PATH + "/getNodePathsByNodeName";
        ResponseMsg<List<String>> responseMsg = restClient.doPost(SERVICE_CODE, url, value, new ParameterizedTypeReference<ResponseMsg<List<String>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 树查询方法
     * @author 钟一华
     * @param orgId
     * @param deptId
     * @param id
     * @param paths
     * @param value
     * @return
     * @throws Exception List<TreeNode>
     * @throws
     */
    public List<TreeNode> findSearchTreeNode(String orgId, String deptId, String id, List<String> paths, String value) {
        String url = BASE_PATH + "/findSearchTreeNode";
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        map.put("deptId",deptId);
        map.put("id",id);
        map.put("paths",paths);
        map.put("value",value);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, value, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 递归获取tree当前节点下数据
     * @author 钟一华
     * @param id
     * @param
     * @return List<TreeNode>
     * @throws
     */
    public List<TreeNode> recurFindTreeNodesByParentId(String id, String orgId, String deptId, int i) {
        String url = BASE_PATH + "/recurFindTreeNodesByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("orgId",orgId);
        map.put("deptId",deptId);
        map.put("i",i);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     *
     * @param id
     * @param orgId
     * @param deptId
     * @param sysId
     */
    public void delTree(String id, String orgId, String deptId, String sysId) {
        String url = BASE_PATH + "/delTree";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("orgId",orgId);
        map.put("deptId",deptId);
        map.put("sysId",sysId);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
    }

    /**
     * 判断新建节点名称是否已存在
     * @param parentId
     * @param archiveClassNodeName
     * @return
     */
    public boolean checkNodeNameByIntfs(String parentId, String archiveClassNodeName) {
        String url = BASE_PATH + "/checkNodeNameByIntfs";
        Map<String,Object> map = new HashMap<>();
        map.put("parentId",parentId);
        map.put("archiveClassNodeName",archiveClassNodeName);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 获得节点路径
     * @param parentId
     * @return
     */
    public String getNodePathByParentId(String parentId) {
        String url = BASE_PATH + "/getNodePathByParentId";
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, parentId, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     *添加tree节点
     * @param lac
     */
    public void saveTree(LantopArchiveClass lac) {
        String url = BASE_PATH + "/saveTree";
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, lac, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
    }

    /**
     * 获得向上模板数据id
     * @param parentId
     * @return
     */
    public List<LantopArchiveClass> getModelData(String parentId) {
        String url = BASE_PATH + "/getModelData";
        ResponseMsg<List<LantopArchiveClass>> responseMsg = restClient.doPost(SERVICE_CODE, url, parentId, new ParameterizedTypeReference<ResponseMsg<List<LantopArchiveClass>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 根据id获得树节点对应数据
     * @param id
     * @return
     */
    public LantopArchiveClass getArchiveclassmanagerById(String id) {
        String url = BASE_PATH + "/getArchiveclassmanagerById";
        ResponseMsg<LantopArchiveClass> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<LantopArchiveClass>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     *复制树节点和节点的子节点
     * @param parentId
     * @param copyId
     * @param orgId
     * @param deptId
     * @param sysId
     * @param userId
     * @param ip
     */
    public void saveCopyTreeData(String parentId, String copyId, String orgId, String deptId, String sysId, String userId, String ip) {
        String url = BASE_PATH + "/saveCopyTreeData";
        Map<String,Object> map = new HashMap<>();
        map.put("parentId",parentId);
        map.put("copyId",copyId);
        map.put("orgId",orgId);
        map.put("deptId",deptId);
        map.put("sysId",sysId);
        map.put("userId",userId);
        map.put("ip",ip);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
    }

    /**
     *通过parentId获得子节点数据
     * @param page
     * @param parentId
     * @return
     */
    public Paging<LantopArchiveClass> getTreeDataByParentId(Paging<LantopArchiveClass> page, String parentId) {
        String url = BASE_PATH + "/getTreeDataByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("parentId",parentId);
        ResponseMsg<Paging<LantopArchiveClass>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Paging<LantopArchiveClass>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     *判断目标节点是否存在重复数据
     * @param copyId
     * @param parentId
     * @return
     */
    public boolean isRepeatNode(String copyId, String parentId) {
        String url = BASE_PATH + "/isRepeatNode";
        Map<String,Object> map = new HashMap<>();
        map.put("copyId",copyId);
        map.put("parentId",parentId);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 通过节点集合返回数据节点信息
     * @param modelParam
     * @return
     */
    public Map<String, Object> getDataNodeInfoByParam(Map<String, Object> modelParam) {
        String url = BASE_PATH + "/getDataNodeInfoByParam";
        Map<String,Object> map = new HashMap<>();
        map.put("modelParam",modelParam);
        ResponseMsg<Map<String, Object>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Map<String, Object>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }
}
